<template>
  <el-form :model="form" label-width="80px">
    <el-form-item label="组件">
      <el-select v-model="form.componentKey" placeholder="选择组件">
        <el-option v-for="item in componentKeys" :key="item" :label="item" :value="item" />
      </el-select>
    </el-form-item>
    <el-form-item label="图标">
      <span class="el-dropdown-link" @click="showDialog">
        <svg-icon :icon-class="form.icon || ''" />
        <i class="el-icon-arrow-down el-icon--right" />
      </span>
    </el-form-item>
    <el-form-item label="缓存">
      <el-switch v-model="form.cache" />
    </el-form-item>
    <el-form-item label="标签固定">
      <el-switch v-model="form.affix" />
    </el-form-item>
    <el-form-item label="面包屑">
      <el-switch v-model="form.breadcrumb" />
    </el-form-item>
    <IconDialog ref="IconDialog" v-model="form.icon" />
  </el-form>
</template>

<script>
import { keyRoutes } from '@/router'
import IconDialog from './IconDialog.vue'

export default {
  name: 'ManagerSystemForm',
  components: { IconDialog },
  props: {
    value: {
      type: String,
      default: () => '{}'
    }
  },
  data() {
    return {
      componentKeys: Object.keys(keyRoutes),
      icons: [],
      form: JSON.parse(this.value || '{}'),
      isNextWatch: true
    }
  },
  watch: {
    value(val) {
      this.isNextWatch = false
      this.form = JSON.parse(val || '{}')
    },
    form: {
      deep: true,
      handler: function(val) {
        this.isNextWatch && this.$emit('input', JSON.stringify(val))
        this.isNextWatch = true
      }
    }
  },
  methods: {
    showDialog() {
      (this.$refs.IconDialog || { showDialog: () => {} }).showDialog()
    }
  }
}
</script>
